package com.shuigu.ai.langgraph4j.tools;

import cn.hutool.core.lang.Assert;
import com.shuigu.ai.langgraph4j.model.ImageCategoryEnum;
import com.shuigu.ai.langgraph4j.model.ImageResource;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import dev.langchain4j.community.model.dashscope.WanxImageModel;
import dev.langchain4j.community.model.dashscope.WanxImageSize;
import dev.langchain4j.data.image.Image;
import dev.langchain4j.model.output.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class LogoGeneratorTool {

    @Value("${langchain4j.community.dashscope.chat-model.api-key}")
    private String dashScopeApiKey;

    @Value("${langchain4j.community.dashscope.chat-model.model-name}")
    private String imageModel;

    @Tool("根据描述生成 Logo 设计图片，用于网站品牌标识")
    public List<ImageResource> generateLogos(@P("Logo 设计描述，如名称、行业、风格等，尽量详细") String description) {
        List<ImageResource> logoList = new ArrayList<>();
        try {
            // 构建 Logo 设计提示词
            String logoPrompt = String.format("生成 Logo，Logo 中禁止包含任何文字！Logo 介绍：%s", description);

            WanxImageModel wanxImageModel = WanxImageModel.builder()
                    .size(WanxImageSize.SIZE_1024_1024)
                    .apiKey(dashScopeApiKey)
                    .modelName(imageModel)
                    .build();

            Response<List<Image>> result = wanxImageModel.generate(logoPrompt,1);

            Assert.notNull(result.content(), "生成 Logo 图片失败");
            for (Image image : result.content()) {
                ImageResource logo = ImageResource.builder()
                        .category(ImageCategoryEnum.LOGO)
                        .description(description)
                        .url(image.url().toString())
                        .build();
                logoList.add(logo);
            }

        } catch (Exception e) {
            log.error("生成 Logo 失败: {}", e.getMessage(), e);
        }
        return logoList;
    }
}
